<?php 
	header("Content-Type: text/html; charset=utf-8");
	set_time_limit(0);
?>


<html lang="zh" xml:lang="zh" xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	</head>
<body>
<form method="post" action="2cat.php">
	<table>
		<tr>
			<td colspan="2">
				http://2cat.or.tl/~ericpony/5d.c/采集器
			</td>
		</tr>
		<tr>
			<td width="20%">
				分类地址:
			</td>
			<td>
				<input type="text" name="url" value="http://2cat.csie.org/~ericpony/5d.c/pixmicat.php?mode=module&load=mod_catalog&page=0" />
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<input type="submit" value="GO" />
			</td>
		</tr>
	</table>
</form>
</body>
</html>


<?php
	
	//某个连接
	$link = $_POST['url'];
	if($link == ''){
		exit;
	}
    
    //預定義JS解析器變量
    $tmpjs_path = "";

    $sbgtr = <<<ETT
    LoadModule('jsstd');
    LoadModule('jscrypt');
    var location = "";
    var replace = "";
ETT;

    $sedtr = "Print( replace );";



	//预定义变量
	$default_pag_pre = "http://2cat.csie.org/~ericpony/5d.c/pixmicat.php?mode=module&load=mod_catalog&page=";
	$default_0pag = "http://2cat.csie.org/~ericpony/5d.c/pixmicat.php?mode=module&load=mod_catalog&page=0";
	$base_url = "http://2cat.csie.org/~ericpony/5d.c/src/";
	$ext_name = ".jpg";
	
	//获取首页面内容
	$content = file_get_contents($default_0pag);
	//$content = file_get_contents("1.html");
	//获取总页面数
	$total_pgs = explode("</a>] </td><td><a href",$content);
	$total_pgs = explode(">",$total_pgs[0]);
	$total_pgs = $total_pgs[count($total_pgs) - 1];
	
	//总处理流程，遍历每页
	for ($i = 0; $i <= $total_pgs; $i++) {
		$current_pg_url = $default_pag_pre . $i;
// 		echo $current_pg_url;exit;
		//获取图片列表
		
// 		print_r(get_picurls(file_get_contents($current_pg_url)));exit;
		
		get_realpicurls(get_picurls(file_get_contents($current_pg_url)),$base_url,$ext_name);
		//exit;
		//echo "<br>" . $total_pgs . "<br>";
	}

	//获取图片列表页面的图片显示页面链接
	function get_picurls($content) {
		$pic_links = array();
		$match_str = "/<div class=\"list\"><a href=\"src\/(.*)\" rel=/isU";
		preg_match_all($match_str,$content,$pic_links);
		return $pic_links[1];
	}
	//根据图片列表显示每个图片的下载链接
	function get_realpicurls($pic_links,$base_url,$ext_name) {
        global $sbgtr ,$sedtr;
		foreach ($pic_links as $k) {
			$jp_url = $base_url . $k;
			//获取该页面的JSP用于生成实际链接
			$jp_content = file_get_contents($jp_url);
			//獲取頁面內JSP代碼用於給解析器解析
			$tp = explode("<script>",$jp_content);
			$tp = explode("</script>",$tp[1]);
			$tp = $tp[0];
			$path_info = pathinfo($_SERVER['SCRIPT_FILENAME']);
            $tmpjs_path  = $path_info['dirname'] . "/1.js";
            file_put_contents($tmpjs_path,$sbgtr . replacer($tp) . $sedtr);
			echo $base_url . exec("jshost " . $tmpjs_path);
			//舊有方法，目前已停用
			//$tp = explode("=\"",$jp_content);
			//$tp = explode("\";",$tp[1]);
			//$tp = $tp[0];
			//获取具体的图片URL链接
			//echo make_pic_url($base_url,get_filename($tp),$ext_name);
			echo "<BR>";
			ob_flush();exit;
		}
		unset($jp_content);
	}
	//获取某个页面的具体文件名
	function get_filename($tp) {
		$str_arr = str_split($tp);
		
		//print_r($str_arr);
		
		foreach ($str_arr as $k => $v) {
			$wasc_code = ord($v);
			$str_arr[$k] = $wasc_code % 10;
		}
		//print_r($str_arr);exit;
		
		return implode($str_arr);
	}
	//合成该文件的文件名
	function make_pic_url($base_url,$filename,$extname) {
		return $base_url . $filename . $extname;
	}
	//JS解析器構造
	function replacer($stt){return str_replace(array("|replace|"),array("|replace(\"locahost\");replace=|"),$stt);}
	

?>
